#include "repo_res.h"
#include "repo_item.h"

#include "fileio/fileio.h"

#include "external/hashlib2plus/src/hashlibpp.h"

#include <algorithm>

namespace renderit
{
	namespace messiah
	{
		const std::string& repo_res::get_md5()
		{
			if (md5_.size())
			{
				return md5_;
			}

			std::vector<std::string> md5s;

			std::vector<std::string> res_obj_fnames = fileio::list_dir(dir_);
			for (auto& res_obj_fname : res_obj_fnames)
			{
				std::string path = fileio::join(dir_, res_obj_fname);

				md5wrapper md5_gen;
				md5s.push_back(std::move(md5_gen.getHashFromFile(path)));
			}

			std::sort(md5s.begin(), md5s.end(), [](auto& a, auto& b) {
				return a < b;
			});

			for (uint32 i = 0; i < md5s.size(); ++i)
			{
				if (i > 0)
				{
					md5_ += " ";
				}
				md5_ += md5s[i];
			}

			return md5_;
		}

		uint32 repo_res::get_fsize()
		{
			if (fsize_ > 0)
			{
				return fsize_;
			}

			fsize_ = fileio::get_size(dir_);
			return fsize_;
		}
	}
}